package de.dhbw.minf12.mas;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;

import de.dhbw.minf12.mas.TestInstance.TestInstance;
import de.dhbw.minf12.mas.excel_export.DetailedExcelExporter;
import de.dhbw.minf12.mas.excel_export.ResultExcelExporter;
import de.dhbw.minf12.mas.logic.AbstractTargetFunctionLogic;
import de.dhbw.minf12.mas.logic.ProducerLogic;
import de.dhbw.minf12.mas.logic.SupplierLogic;
import de.dhbw.minf12.mas.voter.LogicalVoter;

public class LogicalAgentTester {
	private static int NUMBER_ROUNDS = 1;

	private List<LogicalVoter> agents;
	private TestInstance instance;
	private ResultExcelExporter resultExporter;
	private DetailedExcelExporter detailExporter;
	
	@Before
	public void setUp() throws Exception {
		instance = new TestInstance();
		agents = new ArrayList<LogicalVoter>();
		resultExporter = new ResultExcelExporter("NochCoolTest");
		detailExporter = new DetailedExcelExporter();
	}
	
	@Test
	public void test() {
		List<int[][]> instanceList = instance.getNext();

		while(instanceList.size() == 2){
			agents.clear();
			AbstractTargetFunctionLogic targetFunction1 = new SupplierLogic(instanceList.get(0));
			agents.add(new LogicalVoter(targetFunction1));
			AbstractTargetFunctionLogic targetFunction2 = new ProducerLogic(instanceList.get(1));
			agents.add(new LogicalVoter(targetFunction2));

			String[] filenames = instance.getFilenames();
			detailExporter.createNewSheet("Approval " + filenames[0] + "-" + filenames[1]);

			int solValue1 = 0;
			int solValue2 = 0;
			int count = 0;
			int pos = 0;
			Permutation perm = new Permutation(agents.get(0).getJobCount());
			perm.sortRandomly();
			//System.out.println(perm.getData().toString());
			boolean negativ = false;
			Permutation pCurrentContract = perm;
			perm.sortRandomly();
			
			for (int round = 0; round < NUMBER_ROUNDS; round++) {
				while (pos < 99 && count < 50000){
				negativ = false;
				count++;
				//System.out.println(count);
				for (LogicalVoter agent : agents) {
					if (!agent.vote(perm, pCurrentContract)) {
						negativ = true;
						break;
					}	
				}
				
				if (!negativ)
				{
					pos++;
					pCurrentContract = perm;
				}
				if (pos > 0)	
				perm = Permutation.switchDedicatedPos(pos, perm);
				else
					perm.sortRandomly();
			}
			solValue1 += targetFunction1.calculateSolutionValue(perm);
			solValue2 += targetFunction2.calculateSolutionValue(perm);
			}
			//System.out.println(perm.getData().toString());
			System.out.println(pos);
			//System.out.println("Agent 1: " + targetFunction1.calculateSolutionValue(perm));
			//System.out.println("Agent 2: " + targetFunction2.calculateSolutionValue(perm));
			resultExporter.addRow(filenames[0], filenames[1], solValue1/NUMBER_ROUNDS,
					solValue2/NUMBER_ROUNDS, (solValue1 + solValue2)/NUMBER_ROUNDS);
			System.out.println("" + filenames[0] + "-" + filenames[1]);
			instanceList = instance.getNext();
		}
		
		try {
			resultExporter.write();
			detailExporter.write();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		instance.close();
		
	}

}
